package com.gem.back.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gem.back.entity.AoaDept;
import com.gem.back.entity.AoaPosition;
import com.gem.back.entity.AoaUser;
import com.gem.back.service.IAoaDeptService;
import com.gem.back.service.IAoaPositionService;
import com.gem.back.service.IAoaUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hhhy
 * @since 2020-02-22
 */
@Controller
@RequestMapping(value = "/dept")
public class AoaDeptController {

    @Autowired
    private IAoaDeptService aoaDeptService;
    @Autowired
    private IAoaUserService aoaUserService;
    @Autowired
    private IAoaPositionService aoaPositionService;



    @RequestMapping("/deptmanage")
    public String deptManage(Model model){
        List<AoaDept> deptList = aoaDeptService.list();
        model.addAttribute("dept",deptList);
        return "user/deptmanage";
    }

    @RequestMapping("/deptedit")
    public String form(Integer deptId,AoaDept aoaDept,Model model){
        if (deptId != null) {
            aoaDept = aoaDeptService.getById(deptId);
        }
        model.addAttribute("deptedit",aoaDept);
        return "user/deptedit";
    }

    @RequestMapping("/save")
    public String saveOrUpdate(AoaDept aoaDept){
        aoaDeptService.saveOrUpdate(aoaDept);
        return "redirect:/dept/deptmanage";
    }

    @RequestMapping("/delete")
    public String deleteDept(Integer deptId){
        aoaDeptService.removeById(deptId);
        return "redirect:/dept/deptmanage";
    }

    //读取部门下员工
    @RequestMapping("/readDept")
    public String readDept(@RequestParam(value = "deptId") Integer deptId, Model model){
        //读取部门信息
        AoaDept dept = aoaDeptService.getById(deptId);

        //读取部门经理
        AoaUser deptmanage = null;
        if (dept.getDeptmanager() != null) {
            deptmanage = aoaUserService.getById(dept.getDeptmanager());
            System.out.println(deptmanage);
            model.addAttribute("deptmanage",deptmanage);
        }


        //读取所有部门
        List<AoaDept> depts = aoaDeptService.list();
        QueryWrapper<AoaPosition> queryWrapper = new QueryWrapper<>();
        queryWrapper.notLike("dept_id","1");
        queryWrapper.notLike("name","经理");
        //读取所有的职位
        List<AoaPosition> positions  = aoaPositionService.list(queryWrapper);

        //查询所有的用户
        List<AoaUser> formaluser = new ArrayList<>();
        //读取部门下员工管理用户职位信息
        List<AoaUser> deptusers =aoaUserService.findUserAndPosititonByDeptId(deptId);

        //排除经理级别用户
        for (AoaUser deptUser : deptusers) {
            AoaPosition position = deptUser.getPosition();
            if (!position.getName().endsWith("经理"))formaluser.add(deptUser);
        }

        model.addAttribute("positions",positions);
        model.addAttribute("depts",depts);
        model.addAttribute("deptuser",formaluser);

        model.addAttribute("dept",dept);
        model.addAttribute("isread",1);
        return "user/deptread";
    }

    //普通员工人事调动
    @RequestMapping("deptandpositionchange")
    public String deptandpositionchange(@RequestParam("positionid") Integer positionid,
                                        @RequestParam("changedeptid") Integer changedeptid,
                                        @RequestParam("userid") Integer userid,
                                        @RequestParam("deptid") Integer deptid,
                                        Model model){
        AoaUser user = new AoaUser();
        user.setUserId(userid);
        user.setPositionId(positionid);
        user.setDeptId(changedeptid);
        //更新用户信息，更改部门和职位
        aoaUserService.updateById(user);
        return "redirect:/dept/readDept?deptId=" + deptid;
    }

}
